package BaseUse;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test_03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("反射.Student");
        //1.通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //2.获得注解的value的值.
        Tablepeng tablekuang = (Tablepeng) c1.getAnnotation(Tablepeng.class);
        String value = tablekuang.value();
        System.out.println(value);
        //3.获得指定属性的注解
        Field name = c1.getDeclaredField("name");
        Fieldpeng annotation = name.getAnnotation(Fieldpeng.class);
        System.out.println(annotation.columnname());
        System.out.println(annotation.type());
        System.out.println(annotation.length());
    }
}

@Tablepeng(value = "db_student")
class Student {
    @Fieldpeng(columnname = "db_id", type = "int", length = 10)
    private int id;
    @Fieldpeng(columnname = "db_age", type = "int", length = 10)
    private int age;
    @Fieldpeng(columnname = "db_name", type = "int", length = 10)
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablepeng {
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldpeng {
    String columnname();

    String type();

    int length();
}

